<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rule;
use Route;
use App\Rules\PidRule;
use App\Models\AdminPermission as Permission;
use Illuminate\Foundation\Http\FormRequest;

class PermissionRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->route('permission') ? $this->route('permission')->id : null; //获取当前需要排除的id
        return [
            'pid' => [
                'required',
                new PidRule(new Permission, $id),
            ],
            'title' => 'required|max:50',
            'name' => 'required|max:50|unique:permissions,name,' . $id,
            'sort' => 'required|numeric',
            'icon' => 'nullable|max:50',
            'type' => 'required|boolean',
            'status' => 'required|boolean',
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'pid.required' => '所属分类不能为空.',
            'title.required' => '名称不能为空.',
            'title.max' => '名称不能大于50位字符.',
            'name.required' => '标识不能为空.',
            'name.max' => '标识不能大于50位字符.',
            'name.unique' => '标识已存在.',
            'sort.required' => '排序不能为空.',
            'sort.numeric' => '排序只能包含数字字符.',
            'icon.max' => 'icon不能大于50位字符.',
            'type.required' => '类型不能为空.',
            'type.boolean' => '类型操作有误.',
            'status.required' => '状态不能为空.',
            'status.boolean' => '状态操作有误.',
        ];
    }
}
